import { ClassName, Events } from "@/Constants";
import { IRelativePos } from "../../../Interface";
import CtrlPnt from "./CtrlPnt";
import Feature from "../../Feature";

// rotate 旋转控制点元素
class RCtrlPnt extends CtrlPnt {
    getPoint: (args?: any) => IRelativePos;
    lastAngle: number;

    constructor(parent: Feature, fn: () => IRelativePos, width = 14, height?:number) {   // 相对坐标
        const pos = fn();
        super(parent, pos.x, pos.y, width, height);
        this.className = ClassName.RCTRLPNT;
        this.lastAngle = parent.angle;
        this.adsorbTypes = []
        this.getPoint = fn;
        this.parent.on(Events.TRANSLATE, this.updatePosByParent)
        this.parent.on(Events.RESIZE, this.updatePosByParent)
    }

    // 更新自己的位置, 除了自己正在被操控时
    updatePosByParent = (...args: any) => {
        if (this.isFocused) return;
        const { x, y } = this.getPoint(...args);
        this.pointArr = [{ x, y }]
    }

    destroy(): void {
        super.destroy();
        this.parent.off(Events.TRANSLATE, this.updatePosByParent)
        this.parent.off(Events.RESIZE, this.updatePosByParent)
    }
}

export default RCtrlPnt;